//
//  M1SettingUpVC.m
//  NewMarryU
//
//  Created by Cedar on 2018/7/19.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1SettingUpVC.h"
#import "M1EditDetailsCell.h"
#import "M1FeedBackVC.h"
#import "M1BlackListVC.h"
#import "M1LoginVC.h"

@interface M1SettingUpVC ()

@end

@implementation M1SettingUpVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"设置";
    
    [self.m1_tableView registerNib:[UINib nibWithNibName:@"M1EditDetailsCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:m1_tableViewCellIdentifier];

    UILabel *version = [[UILabel alloc] init];
    version.text = [NSString stringWithFormat:@"版本号 %@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
    version.textColor = m1_color(0x666666);
    version.font = m1_font(15);
    [self.view addSubview:version];
    
    [version mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.offset(iPhoneX?-54:-20);
    }];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        M1FeedBackVC *feedBackVC = [[M1FeedBackVC alloc] init];
        [self.navigationController pushViewController:feedBackVC animated:YES];
    }else if (indexPath.row == 1){
        M1BlackListVC *blackVC = [[M1BlackListVC alloc] init];
        [self.navigationController pushViewController:blackVC animated:YES];
    }else if (indexPath.row == 2){
        [self m1_cleanAtPath];
    }else{
        [M1Alert m1_alertWithTitle:nil message:@"是否退出登录?" buttonTitles:@[@"取消",@"确定"] action:^(NSInteger index) {
            if (index == 1) {
                
                [M1HttpNet requestWithMethod:@"app/user/logout" params:nil success:^(id resDic, NSInteger code, NSString *message) {
                    [MobClick profileSignOff];
                    [M1User remove];
                    [[EMClient sharedClient] logout:YES];
                    M1ShuntVC *m1_shunVC = [M1ShuntVC new];
                    M1NavigationController *m1_nav = [[M1NavigationController alloc]initWithRootViewController:m1_shunVC];
                    [UIApplication sharedApplication].delegate.window.rootViewController = m1_nav;
                    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
                } fail:^(id resDic, NSInteger code, NSString *message) {
                    [M1Alert m1_alertWithTitle:nil message:message];
                } showAni:YES];
                
            }
        }];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    M1EditDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:m1_tableViewCellIdentifier];
        
    cell.m1_title.text = @[@"意见反馈", @"小黑屋", @"清理缓存", @"退出登录"][indexPath.row];
    
    cell.m1_title.textColor = m1_color(0x333333);
    
    if ([cell.m1_title.text isEqualToString:@"清理缓存"]) {
        cell.m1_content.hidden = NO;
        cell.m1_across.hidden = YES;
        cell.m1_content.text = [self m1_folderSizeAtPath];
    }else{
        cell.m1_across.hidden = NO;
        cell.m1_content.hidden = YES;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

#pragma mark - 清除缓存
- (NSString *)m1_folderSizeAtPath{
    CGFloat folderSize = 0.0;
    
    //获取路径
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)firstObject];
    
    //获取所有文件的数组
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachePath];
    
    NSLog(@"文件数：%ld",files.count);
    
    for(NSString *path in files) {
        
        NSString*filePath = [cachePath stringByAppendingString:[NSString stringWithFormat:@"/%@",path]];
        
        //累加
        folderSize += [[NSFileManager defaultManager]attributesOfItemAtPath:filePath error:nil].fileSize;
    }
    //转换为M为单位
    CGFloat sizeM = folderSize /1024.0/1024.0;
    
    return [NSString stringWithFormat:@"%.0fMB", sizeM];
}

- (void)m1_cleanAtPath{
    //获取路径
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)objectAtIndex:0];
    
    //返回路径中的文件数组
    NSArray*files = [[NSFileManager defaultManager]subpathsAtPath:cachePath];
    
    NSLog(@"文件数：%ld",[files count]);
    for(NSString *p in files){
        NSError*error;
        
        NSString*path = [cachePath stringByAppendingString:[NSString stringWithFormat:@"/%@",p]];
        
        if([[NSFileManager defaultManager]fileExistsAtPath:path])
        {
            BOOL isRemove = [[NSFileManager defaultManager]removeItemAtPath:path error:&error];
            if(isRemove) {
                NSLog(@"清除成功");
                __weak typeof(self)weakSelf = self;
                [M1Alert m1_alertWithTitle:nil message:@"清理成功"];
                [weakSelf.m1_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
            }else{
                NSLog(@"清除失败");
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
